package com.yearno.common.utils

enum class CompareUtil {
    INSTANCE;

    /**
     * 比较两个字符串
     */
    fun compareVersion(v1: String, v2: String): Int {
        if (v1 == v2) {
            return 0
        }
        val version1 = v1.split("\\.").toTypedArray()
        val version2 = v2.split("\\.").toTypedArray()
        var index = 0
        val minLen = Math.min(version1.size, version2.size)
        var diff: Long = 0
        while (index < minLen && version1[index].toLong() - version2[index].toLong().also {
                diff = it
            } == 0L) index++
        return if (diff == 0L) {
            for (i in index until version1.size) if (version1[i].toLong() > 0) return 1
            for (i in index until version2.size) if (version2[i].toLong() > 0) return -1
            0
        } else {
            if (diff > 0) 1 else -1
        }
    }

    /**
     * 比较两个字符串的float值大小
     */
    fun compareOdd(old: String, new: String): Int {
        val oldF = if (old.isBlank()) {
            0f
        } else {
            old.toFloat()
        }
        val newF = if (new.isBlank()) {
            0f
        } else {
            new.toFloat()
        }
        return when {
            oldF > newF -> {
                -1
            }
            oldF < newF -> {
                1
            }
            else -> {
                0
            }
        }

    }
}